package com.softwinner.awbt.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TableRow;
import android.widget.TextView;

import com.softwinner.awbt.R;
import com.softwinner.awbt.activity.MainActivity;
import com.softwinner.awbt.listener.MusicListener;
import com.softwinner.awbt.utils.LOG;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * to handle interaction events.
 * Use the {@link MusicFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MusicFragment extends BaseFragment implements MusicListener, View.OnClickListener {

    private MainActivity mActivity;
    private TextView mSongTextView;
    private TextView mSingerTextView;
    private ImageButton mPlayImageButton;
    private ImageButton mNextImageButton;
    private ImageButton mPreviousImageButton;

    private static MusicListener sMusicListener;
    private static final int STATUS_DISCONNECT = 1;
    private static final int STATUS_CONNECT = 2;
    private static final int STATUS_PLAYING = 3;

    private int mStatus;
    private String mSong;
    private String mSinger;

    public MusicFragment() {
        sMusicListener = this;
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment MusicFragment.
     */
    public static MusicFragment newInstance() {
        return new MusicFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        LOG.d("MusicFragment onCreateView");
        View view = inflater.inflate(R.layout.fragment_music, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mSingerTextView = (TextView) view.findViewById(R.id.singer_tv);
        mSongTextView = (TextView) view.findViewById(R.id.song_name_tv);
        mPlayImageButton = (ImageButton) view.findViewById(R.id.play_ib);
        mPreviousImageButton = (ImageButton) view.findViewById(R.id.previous_ib);
        mNextImageButton = (ImageButton) view.findViewById(R.id.next_ib);
        mPlayImageButton.setOnClickListener(this);
        mPreviousImageButton.setOnClickListener(this);
        mNextImageButton.setOnClickListener(this);
        mSingerTextView.setText(mSinger);
        mSongTextView.setText(mSong);
        if (!isDetach()) {
            updateStatus();
        }
    }

    @Override
    public void onAttach(Context context) {
        LOG.d("MusicFragment onAttach");
        super.onAttach(context);
        if (context instanceof MainActivity) {
            mActivity = (MainActivity) context;
        }
    }

    @Override
    public void onDetach() {
        LOG.d("MusicFragment onDetach");
        mActivity = null;
        sMusicListener = null;
        super.onDetach();
    }

    @Override
    public void onPlay() {
        mStatus = STATUS_PLAYING;
        if (mPlayImageButton != null) {
            mPlayImageButton.setImageResource(R.drawable.ic_pause);
            mPlayImageButton.setEnabled(true);
        }
        mActivity.getMusicInfo();
    }

    @Override
    public void onPauseMusic() {
        mStatus = STATUS_CONNECT;
        if (mPlayImageButton != null) {
            mPlayImageButton.setImageResource(R.drawable.ic_play);
            mPlayImageButton.setEnabled(true);
        }
    }

    @Override
    public void onNext() {

    }

    @Override
    public void onPrevious() {

    }

    @Override
    public void onStatus(int status) {
        if (isDetach()) {
            mStatus = status;
            return;
        }

        if (status != mStatus) {
            mStatus = status;
            updateStatus();
        }

    }

    private void updateStatus() {
        switch (mStatus) {
            case STATUS_DISCONNECT:
                mPlayImageButton.setEnabled(false);
                mNextImageButton.setEnabled(false);
                mPreviousImageButton.setEnabled(false);
                mPlayImageButton.setImageResource(R.drawable.ic_play);
                break;
            case STATUS_CONNECT:
                mPlayImageButton.setEnabled(true);
                mNextImageButton.setEnabled(true);
                mPreviousImageButton.setEnabled(true);
                mPlayImageButton.setImageResource(R.drawable.ic_play);
                break;
            case STATUS_PLAYING:
                mPlayImageButton.setEnabled(true);
                mNextImageButton.setEnabled(true);
                mPreviousImageButton.setEnabled(true);
                mPlayImageButton.setImageResource(R.drawable.ic_pause);
                break;
        }

        if (TextUtils.isEmpty(mSinger)) {
            mActivity.getMusicInfo();

        }

    }

    @Override
    public void onMusicInfo(String name, String artist) {
        mSinger = artist;
        mSong = name;

        if (TextUtils.isEmpty(name)) {
            mSong = getString(android.R.string.unknownName);
        } else {
            mSong = name;
        }

        if (TextUtils.isEmpty(artist)) {
            mSinger = getString(android.R.string.unknownName);
        } else {
            mSinger = artist;
        }

        if (mSingerTextView == null) {
            return;
        }

        mSongTextView.setText(mSong);
        mSingerTextView.setText(mSinger);
        mPlayImageButton.setImageResource(R.drawable.ic_pause);
    }

    @Override
    public void onConnected() {
        mStatus = STATUS_CONNECT;
        mPlayImageButton.setEnabled(true);
        mNextImageButton.setEnabled(true);
        mPreviousImageButton.setEnabled(true);
    }

    @Override
    public void onDisconnected() {
        mStatus = STATUS_DISCONNECT;
        mPlayImageButton.setEnabled(false);
        mNextImageButton.setEnabled(false);
        mPreviousImageButton.setEnabled(false);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.play_ib:
                mPlayImageButton.setEnabled(false);
                mActivity.playOrPauseMusic();
                break;
            case R.id.next_ib:
                mActivity.toNextMusic();
                break;
            case R.id.previous_ib:
                mActivity.toPreviousMusic();
                break;
        }
    }


    public static MusicListener getMusicListener() {
        return sMusicListener;
    }

}
